package com.jiyun.alipay.util;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;

/**
 * 微信支付工具类
 *
 */
public class WxChatPayCommonUtil {
        /**
         * 发送 http 请求
         * @param requestUrl    请求路径
         * @param requestMethod 请求方式(GET/POST/PUT/DELETE/...)
         * @param outputStr     请求参数体
         * @return 结果信息
         */
        public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
            try {
                URL url = new URL(requestUrl);
                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                // 设置请求方式（GET/POST）
                conn.setRequestMethod(requestMethod);
                conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
                // 当outputStr不为null时向输出流写数据
                if (null != outputStr) {
                    OutputStream outputStream = conn.getOutputStream();
                    // 注意编码格式
                    outputStream.write(outputStr.getBytes("UTF-8"));
                    outputStream.close();
                }
                // 从输入流读取返回内容
                InputStream inputStream = conn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String str = null;
                StringBuffer buffer = new StringBuffer();
                while ((str = bufferedReader.readLine()) != null) {
                    buffer.append(str);
                }
                // 释放资源
                bufferedReader.close();
                inputStreamReader.close();
                inputStream.close();
                inputStream = null;
                conn.disconnect();
                return buffer.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * 获取ip
         * @param request 请求
         * @return ip 地址
         */
        public static String getIp(HttpServletRequest request) {
            if (request == null) {
                return "";
            }
            String ip = request.getHeader("X-Requested-For");
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Forwarded-For");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip;
        }

        /**
         * 从流中读取微信返回的xml数据
         * @param httpServletRequest
         * @return
         * @throws IOException
         */
        public static String readXmlFromStream(HttpServletRequest httpServletRequest) throws IOException {
            InputStream inputStream = httpServletRequest.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            final StringBuffer sb = new StringBuffer();
            String line = null;
            try {
                while ((line = bufferedReader.readLine()) != null) {
                    sb.append(line);
                }
            } finally {
                bufferedReader.close();
                inputStream.close();
            }

            return sb.toString();
        }

        /**
         * 设置返回给微信服务器的xml信息
         * @param returnCode
         * @param returnMsg
         * @return
         */
        public static String setReturnXml(String returnCode, String returnMsg) {
            return "<xml><return_code><![CDATA[" + returnCode + "]]></return_code><return_msg><![CDATA[" + returnMsg + "]]></return_msg></xml>";
        }

}

